package com.ae.shield.client.gui;

import com.ae.shield.common.items.ItemList;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import com.ae.shield.ModLib;
import com.ae.shield.client.gui.util.GuiHelper;
import com.ae.shield.common.gui.container.ShieldContainer;
import com.ae.shield.common.items.ItemIntegratedShield;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;

public class ShieldScreen extends ContainerScreen<ShieldContainer> {
    private static final ResourceLocation GUI = new ResourceLocation(ModLib.MOD_ID, "textures/gui/shield.png");
    private final GuiHelper guiHelper;
    private boolean isWorking = false;

    public ShieldScreen(ShieldContainer screenContainer, PlayerInventory inventory, ITextComponent titleIn) {
        super(screenContainer, inventory, titleIn);
        guiLeft = 0;
        guiTop = 0;
        xSize = 176;
        ySize = 174;
        guiHelper = new GuiHelper(this, xSize, ySize);
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(MatrixStack matrixStack, float partialTicks, int x, int y) {
        RenderSystem.color4f(1f, 1f, 1f, 1f);
        minecraft.textureManager.bindTexture(GUI);

        int x1 = (width - xSize) / 2;
        int y1 = (height - ySize) / 2;
        blit(matrixStack, x1, y1, 0, 0, xSize, ySize);

        if (isWorking) {
            blit(matrixStack, x1 + 155, y1 + 13, 176, 19, 10, 18);
        } else {
            blit(matrixStack, x1 + 148, y1 + 10, 102, 10, 24, 24);
            blit(matrixStack, x1 + 158, y1 + 34, 10, 34, 4, 33);
            blit(matrixStack, x1 + 158, y1 + 65, 176, 0, 4, 3);
            blit(matrixStack, x1 + 155, y1 + 13, 176, 37, 10, 18);
            blit(matrixStack, x1 + 153, y1 + 47, 176, 55, 14, 20);
        }

        blit(matrixStack, x1 + 156, y1 + 14, 176, 3, 8, 16);
    }

    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        renderBackground(matrixStack);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
        renderHoveredTooltip(matrixStack, mouseX, mouseY);
        isWorking = isShieldWorking();
        if (guiHelper.isMouseover(mouseX, mouseY, 152, 47, 16, 30)) {
            List<ITextComponent> list = new ArrayList();
            if (isWorking) {
                list.add(new TranslationTextComponent("screen.ae.shield.shield_screen.on"));
            } else {
                list.add(new TranslationTextComponent("screen.ae.shield.shield_screen.off1"));
                list.add(new TranslationTextComponent("screen.ae.shield.shield_screen.off2"));
            }
            func_243308_b(new MatrixStack(), list, mouseX, mouseY);
        }

    }

    @Override
    protected void drawGuiContainerForegroundLayer(MatrixStack matrixStack, int x, int y) {
        font.func_243248_b(matrixStack, playerInventory.getDisplayName(), (float) playerInventoryTitleX, (float) playerInventoryTitleY + 10, 4210752);
        font.func_243248_b(matrixStack, new TranslationTextComponent("item.aeshield.shield"), 8f, 5.5f, 4210752);
    }

    private boolean isShieldWorking() {
        ItemStack heldItem = playerInventory.player.getHeldItemMainhand();
        ItemStack heldItemOffhand = playerInventory.player.getHeldItemOffhand();
        int isWorking = -2;

        while (isWorking < 0) {
            if (!heldItem.isEmpty() && heldItem.getItem() instanceof ItemIntegratedShield) {
                isWorking = ((ItemIntegratedShield) heldItem.getItem()).isWorking(heldItem);
                break;
            } else if (!heldItemOffhand.isEmpty() && heldItemOffhand.getItem() instanceof ItemIntegratedShield) {
                isWorking = ((ItemIntegratedShield) heldItemOffhand.getItem()).isWorking(heldItemOffhand);
                break;
            }
            isWorking++;
            heldItem = playerInventory.player.getHeldItemOffhand();
        }

        return isWorking == 1;
    }
}
